package com.jinke.api.modules.app.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinke.api.modules.app.mapper.UserAddressMapper;
import com.jinke.api.modules.app.model.UserAddress;
import com.jinke.api.modules.app.request.AddressQueryRequest;
import com.jinke.api.modules.app.service.UserAddressService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 承运商平台 服务实现类
 * </p>
 *
 * @author macro
 * @since 2024-01-24
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {

    @Override
    public Page<UserAddress> list(AddressQueryRequest request) {
        Page<UserAddress> page = new Page<>(request.getPageNum(), request.getPageSize());
        QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
        String keyword = request.getKeyword();
        List<String> ids = request.getIds();
        wrapper.lambda()
                .eq(request.getUserId() != null, UserAddress::getUserId, request.getUserId())
                .eq(CollectionUtil.isNotEmpty(ids), UserAddress::getId, ids)
                .and(keyword != null, w -> {
                    w.like(UserAddress::getTitle, keyword).
                            or().like(UserAddress::getPersonName, keyword).
                            or().like(UserAddress::getPhone, keyword).
                            or().like(UserAddress::getAddressLine1, keyword).
                            or().like(UserAddress::getAddressLine2, keyword);
                })
                .orderByDesc(UserAddress::getId);

        return page(page, wrapper);
    }
}
